package studentManage;

import java.util.*;

public class test {

    public static void main(String[] args) {
        List data = new ArrayList();
        Person a = new Admin("0", "admin", "12346", Sex(), Age(), 2);
        Person t1 = new Teacher("语文", "1", name(), code(), Sex(), Age(), 1);
        Person t2 = new Teacher("英语", "2", name(), code(), Sex(), Age(), 1);
        Person t3 = new Teacher("数学", "3", name(), code(), Sex(), Age(), 1);
        Person s1 = new Student("土木", Score(), Score(), Score(), "4", name(), code(), Sex(), Age(), 0);
        Person s2 = new Student("计算机", Score(), Score(), Score(), "4", name(), code(), Sex(), Age(), 0);
        Person s3 = new Student("美术", Score(), Score(), Score(), "4", name(), code(), Sex(), Age(), 0);
        Person s4 = new Student("音乐", Score(), Score(), Score(), "4", name(), code(), Sex(), Age(), 0);
        Person s5 = new Student("数学", Score(), Score(), Score(), "4", name(), code(), Sex(), Age(), 0);

        data.add(a);
        data.add(t1);
        data.add(t2);
        data.add(t3);
        data.add(s1);
        data.add(s2);
        data.add(s3);
        data.add(s4);
        data.add(s5);



        printList(data);

        while (true){
            Scanner in = new Scanner(System.in);
            System.out.println("请输入你的用户名: ");
            Person p = new Person();
            p.setUsername(in.next());
            System.out.println("请输入用户名对应的密码: ");
            p.setPassword(in.next());



            if (p.getUsername().equals(a.getUsername()) && p.getPassword().equals(p.getPassword())) {
                int k = 0;
                System.out.println("请选择你需要的功能: ");
                System.out.println("1. 增加学生及老师的信息");
                System.out.println("2. 修改学生及老师的信息");
                System.out.println("3. 删除学生及老师的信息");
                System.out.println("其它则退出");
                k = in.nextInt();
                if (k == 1){
                    Person p3 = new Person();
                    System.out.println("请输入number：");
                    p3.setNumber(in.next());
                    System.out.println("请输入username：");
                    p3.setUsername(in.next());
                    System.out.println("请输入password：");
                    p3.setPassword(in.next());
                    System.out.println("请输入sex：");
                    p3.setSex(in.next());
                    System.out.println("请输入age：");
                    p3.setAge(in.nextInt());
                    System.out.println("请输入role：");
                    p3.setRole(in.nextInt());
                    System.out.println("请输入kecheng：");
                    p3.setKecheng(in.next());
                    System.out.println("请输入zhuanYe：");
                    p3.setZhuanYe(in.next());
                    add(p3, data);
                    printList(data);
                } else if (k == 2){
                    System.out.println("你要修改的是: 1 学生   2 老师   3 管理员");
                    int choose = in.nextInt();
                    if (choose == 1) {
                        Person p3 = new Student();
                        System.out.println("请输入number：");
                        p3.setNumber(in.next());
                        System.out.println("请输入username：");
                        p3.setUsername(in.next());
                        System.out.println("请输入password：");
                        p3.setPassword(in.next());
                        System.out.println("请输入sex：");
                        p3.setSex(in.next());
                        System.out.println("请输入age：");
                        p3.setAge(in.nextInt());
                        System.out.println("请输入role：");
                        p3.setRole(in.nextInt());
                        System.out.println("请输入zhuanYe：");
                        p3.setZhuanYe(in.next());
                        modify(p3, data, choose);
                        printList(data);
                    } else if (choose == 2){
                        Person p3 = new Teacher();
                        System.out.println("请输入number：");
                        p3.setNumber(in.next());
                        System.out.println("请输入username：");
                        p3.setUsername(in.next());
                        System.out.println("请输入password：");
                        p3.setPassword(in.next());
                        System.out.println("请输入sex：");
                        p3.setSex(in.next());
                        System.out.println("请输入age：");
                        p3.setAge(in.nextInt());
                        System.out.println("请输入role：");
                        p3.setRole(in.nextInt());
                        System.out.println("请输入zhuanYe：");
                        p3.setZhuanYe(in.next());
                        System.out.println("请输入kecheng：");
                        p3.setKecheng(in.next());
                        modify(p3, data, choose);
                        printList(data);
                    } else if (choose == 3) {
                        Person p3 = new Admin();
                        System.out.println("请输入number：");
                        p3.setNumber(in.next());
                        System.out.println("请输入username：");
                        p3.setUsername(in.next());
                        System.out.println("请输入password：");
                        p3.setPassword(in.next());
                        System.out.println("请输入sex：");
                        p3.setSex(in.next());
                        System.out.println("请输入age：");
                        p3.setAge(in.nextInt());
                        System.out.println("请输入role：");
                        p3.setRole(in.nextInt());
                        modify(p3, data, choose);
                        printList(data);
                    }
                } else if (k == 3){
                    Person p3 = new Person();
//                    System.out.println("请输入number：");
//                    p3.setNumber(in.next());
                    System.out.println("请输入username：");
                    p3.setUsername(in.next());
                    System.out.println("请输入password：");
                    p3.setPassword(in.next());
//                    System.out.println("请输入sex：");
//                    p3.setSex(in.next());
//                    System.out.println("请输入age：");
//                    p3.setAge(in.nextInt());
//                    System.out.println("请输入role：");
//                    p3.setRole(in.nextInt());
//                    System.out.println("请输入kecheng：");
//                    p3.setKecheng(in.next());
//                    System.out.println("请输入zhuanYe：");
//                    p3.setZhuanYe(in.next());
                    delete(data, p3.getUsername(), p3.getPassword());
                    printList(data);
                }
            }

        }

    }

    private static void printList(List c) {
        Iterator<Person> p = c.iterator();
        while (p.hasNext()) {
            Person per = (Person) p.next();
            System.out.println(per.toString());
        }
    }

    private static String code() {
        Random r = new Random();
        String name = "";
        for (int i = 0; i < 6; i++){
            char a = (char)(r.nextInt(10) + 48);
            name += a;
        }
        return name;
    }

    public static String name(){

        Random random = new Random();
        String name = "";
        for (int i = 0; i < 5; i++){
            char a = (char)(random.nextInt(26) + 65);
            name += a;
        }
        return name;
    }

    public static Integer Score(){
        Random random = new Random();
        return 40 + random.nextInt(61);
    }

    public static String Sex(){
        return (new Random().nextInt(20)) % 2 == 0 ? "男" : "女";
    }

    public static Integer Age(){
        return (new Random().nextInt(5) + 19);
    }

    public static int add(Person p, List list){

        list.add(p);
        System.out.println("已增加成员");
        return 1;
    }

    public static int delete(List list, String username, String password){
        for (int i = 0; i < list.size(); i++){
            Person p2 = (Person) list.get(i);
            if (p2.getUsername().equals(username) && p2.getPassword().equals(password)){
                list.remove(p2);
                return 1;
            }
        }
        System.out.println("没有你要删除的数据");
        return 0;
    }

    public static int modify(Person p, List list, int choose){

        for (int i = 0; i < list.size(); i++){
            Person p2 = (Person) list.get(i);
            System.out.println(p.toString());
            if (p2.getPassword().equals(p.getPassword()) && p2.getUsername().equals(p.getUsername())){
                if (choose == 1) {
                    p2.setAge(p.getAge());
                    p2.setNumber(p.getNumber());
                    p2.setSex(p.getSex());
                    p2.setRole(p.getRole());
                    p2.setZhuanYe(p.getZhuanYe());
                }else if (choose == 2){
                    p2.setAge(p.getAge());
                    p2.setKecheng(p.getKecheng());
                    p2.setNumber(p.getNumber());
                    p2.setSex(p.getSex());
                    p2.setRole(p.getRole());
                }else if (choose == 3) {
                    p2.setAge(p.getAge());
                    p2.setNumber(p.getNumber());
                    p2.setSex(p.getSex());
                    p2.setRole(p.getRole());
                }
                return 1;
            }
        }
        System.out.println("没有你要修改的数据");
        return 0;
    }
}

